package com.mouse.damo.util;

import java.io.*;

public class BMPReader {

    /**
     * 图片位数
     *
     * @param src
     * @return
     * @throws IOException
     */

    public static int ReadBMPPic(String src) {
        int bit = 0;
        try {
            FileInputStream fis = new FileInputStream(src);
            bit = ReadBMPPic(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bit;
    }

    /**
     * 图片位数
     *
     * @param inputStream
     * @return
     * @throws IOException
     */

    public static int ReadBMPPic(InputStream inputStream) {
        int bit = 0;
        try {
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //丢掉文件头信息
            bis.skip(28);

            //获取长度与宽度
            byte[] b = new byte[2];
            bis.read(b);
            bit = (b[1] & 0xff) << 8 | b[0] & 0xff;
            System.out.println("图片位数：" + bit);
            bis.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bit;
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        ReadBMPPic("D:\\tmp\\santai.bmp");
        ReadBMPPic("D:\\tmp\\test.bmp");
        ReadBMPPic("D:\\tmp\\test1.bmp");
        ReadBMPPic("D:\\tmp\\test2.bmp");
        ReadBMPPic("D:\\tmp\\test3.bmp");
        ReadBMPPic("D:\\tmp\\test4.bmp");
        ReadBMPPic("D:\\tmp\\test5.bmp");
    }

}